<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>home</title>
	<link rel="stylesheet" type="text/css" href="${ctx}/themes/gray/easyui.css">
	<script type="text/javascript" src="${ctx}/script/jquery.min.js"></script>
	<script type="text/javascript" src="${ctx}/script/jquery.easyui.min.js"></script>
	
	<link rel="stylesheet" type="text/css" href="${ctx}/script/ztree/jquery.ztree.css">
	<script type="text/javascript" src="${ctx}/script/ztree/jquery.ztree.min.js"></script>
	
	<link rel="stylesheet" type="text/css" href="${ctx}/script/jqgrid/css/jquery-ui-1.8.2.custom.css">
	<link rel="stylesheet" type="text/css" href="${ctx}/script/jqgrid/css/ui.jqgrid.css">
	<script type="text/javascript" src="${ctx}/script/jqgrid/js/i18n/grid.locale-cn.js"></script>
	<script type="text/javascript" src="${ctx}/script/jqgrid/js/jquery.jqGrid.min.js"></script>
	
	<script type="text/javascript" src="${ctx}/script/lhgdialog/lhgdialog.min.js?s=facebook"></script>
	<script>
	var zTree;
	var setting;

	setting = {
		open:true,
		isSimpleData : true,
   		treeNodeKey : "id",
   		treeNodeParentKey : "pId",
		async: true,
		//asyncUrl: "showGnqxTree.action?czlbId=" + $("#czlbId").val(),  //获取节点数据的URL地址
		asyncUrl: "showGnqxTree.action",  //获取节点数据的URL地址
		asyncParam: ["id", "name"], //获取节点数据时，必须的数据名称，例如：id、name
		callback : {
	      click: zTreeOnClick,
	      asyncSuccess:zTreeOnAsyncSuccess
	    }
	};
	var zNodes =[];
	
	$(document).ready(function(){
		zTree = $("#tree").zTree(setting, zNodes);
	});
		
	function zTreeOnClick(event, treeId, treeNode) {
		//alert(treeNode.tId + ", " + treeNode.id + ", " + treeNode.name);
		//jqGridInit(treeNode.id);
		var URL = 'listXtgngl.action?fid='+treeNode.id;
		$("#fid").val(treeNode.id);
		jQuery("#gridTable").setGridParam({url:URL}).trigger("reloadGrid"); 
	}	
	
	function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
    	zTree.expandAll(true);
	}
	
	function addTreeNode(fid,newNode) {
		var srcNode = zTree.getSelectedNode();
		if (srcNode && srcNode.id == fid) {
			zTree.addNodes(srcNode, newNode);
		} else {
			srcNode = zTree.getNodeByParam("id", fid);
			zTree.addNodes(srcNode, newNode);
		}
	}
	
	function removeTreeNode(id) {
		var srcNode = zTree.getNodeByParam("id", id);
		if (srcNode) {
			if (srcNode.nodes && srcNode.nodes.length > 0) {
				var msg = "要删除的节点是父节点，如果删除将连同子节点一起删掉。\n\n请确认！";
				if (confirm(msg)==true){
					zTree.removeNode(srcNode);
				}
			} else {
				zTree.removeNode(srcNode);
			}
		}	
	}
	
	function updateTreeNode(id,name) {
		var srcNode = zTree.getNodeByParam("id", id);
		srcNode.name = name;
		zTree.updateNode(srcNode);
	}
	
	$(function(){
		$("#fid").val("ROOT");
		jQuery("#gridTable").jqGrid({
		   	url:'listXtgngl.action',
			datatype: 'json',
		   	colNames:['ID','FID','功能名称', '功能地址', '功能类别', '操作'],
		   	colModel:[
		   		{name:'ID', index:'ID',width:150,fixed:true,align:'center',sortable:false,hidden:true},
		   		{name:'FID', index:'FID',width:55,align:'center',sortable:false,hidden:true},
		   		{name:"JDMC",index:'JDMC',width:200,fixed:true,align:'center',sortable:false},
			    {name:"GNDZ",index:'GNDZ',width:80,align:'center',sortable:false},
			    {name:"JDLB",index:'JDLB',width:100,fixed:true,align:'center',sortable:false,formatter:getJdlb},
			    {name:'CZ',index:'ID',width:80,fixed:true,sortable:false, resize:false,formatter:cleverActions,formatoptions:{keys:true}}
		   	],
		   	rowNum: 20,
		   	autowidth: true,
			rowList: [1,2,3,4,5,6,7,8,9,10,15,20,30,40,50,100],
			prmNames: {search: "search"},
			jsonReader: {
				root:"gridModel",
				records: "record",
				repeatitems : false
			},
		   	pager: '#gridpager',
		    viewrecords: true,
		    sortname: 'id',
		    //rownumbers: true,
		    multiselect: true,
		    multiboxonly: true,
		    altRows:true,//隔行变色
		    altclass:'altclass',
		    caption:"系统功能列表"
		});
		$("#gridTable").jqGrid('navGrid','#gridpager',{edit:false,add:false,del:false,search:false,refresh:false});
		
		$("#gridTable").jqGrid().navButtonAdd('#gridpager',{
		   caption:"", 
		   title:"添加新记录",
		   buttonicon:"ui-icon-plus", 
		   onClickButton: function(){ 
		   	   actionOnAdd();
		   }
		});
		
		$("#gridTable").jqGrid().navButtonAdd('#gridpager',{
		   caption:"", 
		   title:"删除所选记录",
		   buttonicon:"ui-icon-trash", 
		   onClickButton: function(){ 
	       	   actionOnDel();
		   }
		}).navSeparatorAdd("#gridpager",{position:"first"});
		doResize();
	});
	
	//设置jqGrid高度
	function doResize() {
		jQuery("#gridTable").jqGrid('setGridHeight',$(document).height()-83);
	}
	
	function getJdlb(cellvalue, options, rowObject) {
	    if(cellvalue == 0) {
	    	return "子系统";
	    } else if(cellvalue == 1) {
	    	return "子模块";
	    } else if(cellvalue == 2) {
	    	return "子功能";
	    } else {
	    	return "";
	    }
	}
	
	//行formatter
	function cleverActions(cellvalue, options, rowObject) {
	    return '<div id="' + options.rowId + '" class="ui-corner-all clever-jqgrid-action clever-jqgrid-edit" style="float:left;margin-left:15px;padding:0px;" title="' + $.jgrid.nav.edittitle + '" onclick="actionOnEditLine(this,event);" onmouseover="actionMouseover(this,event)" onmouseout="actionMouseout(this,event)" ><span class="ui-icon ui-icon-pencil"></span></div>'
	    	 + '<div id="' + options.rowId + '" class="ui-corner-all clever-jqgrid-action clever-jqgrid-del"  style="float:left;margin-left:15px;padding:0px;" title="' + $.jgrid.nav.deltitle  + '" onclick="return actionOnDelLine(this,event);" onmouseover="actionMouseover(this,event)" onmouseout="actionMouseout(this,event)" ><span class="ui-icon ui-icon-trash"></span></div>';
	}
	
	//行formatter鼠标移入样式
	function actionMouseover(el, ev) {
	    $(el).addClass('ui-state-hover');
	    /*
	    if ($.browser.msie) {
	        ev.cancelBubble = true;
	    } else {
	        ev.stopPropagation();
	    }*/
	}
	
	//行formatter鼠标移出样式
	function actionMouseout(el, ev) {
	    $(el).removeClass('ui-state-hover');
	    if ($.browser.msie) {
	        ev.cancelBubble = true;
	    } else {
	        ev.stopPropagation();
	    }
	}
	
	//行操作-修改功能
	var actionOnEditLine = function(el, ev) {  
		var rowId = $(el).attr('id');
	    var rowData = $("#gridTable").getRowData(rowId);
		new jQuery.dialog({id:'1',title: '修改功能',page:'addorModGG_XTGN.action?id='+rowData.ID,width:400,height:300,rang:true,iconTitle:false}).ShowDialog();
	};	

	//行操作-删除角色
	var actionOnDelLine = function(el, ev) {
		if(confirm("您确定要删除该记录吗？")) {
			var rowId = $(el).attr('id');
		    var rowData = $("#gridTable").getRowData(rowId);
		    var srcNode = zTree.getNodeByParam("id", rowData.ID);
			if (srcNode && srcNode.nodes && srcNode.nodes.length > 0) {
				alert("该节点包含子节点，请先删除子节点后再删除！");
			} else {
				var params = {
					"id" : rowData.ID
				};
				var actionUrl = "deleteGG_XTGN.action";
				$.ajax({
					url : actionUrl,
					data : params,
					dataType : "json",
					cache : false,
					error : function(textStatus, errorThrown) {
						alert("系统ajax交互错误: " + textStatus);
					},
					success : function(data, textStatus) {
						if (data.ajaxResult == "success") {
							$("#gridTable").delRowData(rowId);
							removeTreeNode(rowData.ID);
							alert("删除成功!");
						} else {
							alert("删除操作失败!");
						}
					}
				});
			}
			return true;
		} else {
			return false;
		}
	};
	
	//新增功能
	var actionOnAdd = function() {  
		var fid = $("#fid").val();
		//alert(fid);
		new jQuery.dialog({id:'1',title: '新增功能',page:'addorModGG_XTGN.action?fid='+fid,width:400,height:300,rang:true,iconTitle:false}).ShowDialog();
	};  
	
	//删除角色
	var actionOnDel = function() {
		var selr = jQuery('#gridTable').jqGrid('getGridParam','selarrrow');
		var msg = "";
		if(selr.length) {
			if(confirm("您确定要删除所选记录吗？")) {
				var ids = "";
				for(var i = 0; i < selr.length; i++) {
					var rowData = $("#gridTable").getRowData(selr[i]);
					var srcNode = zTree.getNodeByParam("id", rowData.ID);
					if (srcNode && srcNode.nodes && srcNode.nodes.length > 0) {
						msg += rowData.JDMC + "节点,"; 
					} else {
						ids += rowData.ID + ",";
					}
					//ids += $('#gridTable').jqGrid('getCell',selr[i],'ID') + ",";
				}
				ids = ids == "" ? ids : ids.substring(0,ids.length-1);
				//alert(msg + ids);
				var params = {
					"id" : ids
				};
				if(ids.length > 0) {
					var actionUrl = "deleteGG_XTGN.action";
					$.ajax({
						url : actionUrl,
						data : params,
						dataType : "json",
						cache : false,
						error : function(textStatus, errorThrown) {
							alert("系统ajax交互错误: " + textStatus);
						},
						success : function(data, textStatus) {
							if (data.ajaxResult == "success") {
								for(; 0 < selr.length ;) {
									var rowData = $("#gridTable").getRowData(selr[0]);//alert(rowData.ID);
									var srcNode = zTree.getNodeByParam("id", rowData.ID);
									//有子节点的不删除
									if (srcNode && srcNode.nodes && srcNode.nodes.length > 0) {
									} else {
										removeTreeNode(rowData.ID);
										$("#gridTable").delRowData(selr[0]);
									}
								}
								if(msg == "") {
									alert("删除成功！");
								} else if(msg != "" && ids != "") {
									alert(msg + "包含子节点，请先删除子节点后再删除；/r/n其他节点删除成功！");
								}
							} else {
								alert("删除失败！");
							}
						}
					});
				} else {
					alert(msg + "包含子节点，请先删除子节点后再删除！");
				}	
				return true;
			}
		} else {
			alert("至少选择一条记录！");
			return false;
		}
	};
	
	var actionOnSearch = function() {
		new jQuery.dialog({id:'1',title: '查询条件',page:'view/xt/jgqx/gnsq/QueryGG_CZLB.jsp',width:380,height:180,rang:true,iconTitle:false}).ShowDialog();
	};
	</script>
</head>
<body class="easyui-layout">
	<div region="west" border="true" split="true" title="" style="width:200px;padding:0px;border-left:1px solid #99BBE8;">
	<ul id="tree" class="tree"></ul>	
	</div>
	
	<div id="mainPanle" region="center" border="true" border="false" title="">
		<input type="hidden" id ="fid" name="fid" value="">
		<table id="gridTable"></table>
		<div id="gridpager"></div>
	
		<!-- 
		 <div id="tabs" class="easyui-tabs"  fit="true" border="false" >
		 		<div title="Home" style="text-align:center;vertical-align:middle;float:top;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);">
					<img width="100%" heigth="100%" src="${ctx}/themes/gray/images/welcome.jpg"/>
                </div>
         </div>
          -->
	</div>
	<!-- 
	<div region="center" title="MainWorkArea Bar">
		<iframe id="MainWorkArea" name="MainWorkArea" marginWidth="0" marginHeight="0" frameBorder="0" width="100%" height="100%" src="" scrolling="auto"></iframe>
	</div> 
	-->
	<!--
	<div region="east" split="true" title="East" style="width:100px;padding:10px;">east region</div>
	-->
</body>
</html>